package de.cau.apo.problems.identicalscheduling;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import de.cau.apo.problems.IInstance;
import de.cau.apo.utils.InputStreamParser;

/**
 * This class represents an instance of the identical scheduling problem.
 * There is on the one hand an array of jobs with different sizes that has 
 * to be assigned to machines and on the other hand there is a number of
 * available machines to which the jobs can be assigned.
 * 
 * @author Tobias Schwede
 */
public class ISInstance implements IInstance {
    
    // ########### properties ###########
    
	/** the number of machines that are available */
    public int numberMachines = 0;
    /** the array of jobs that need to be assigned to machines */
    public Job[] jobs = null;
    
    // ########### instantiation ###########
    
    /**
     * Default empty constructor
     */
    public ISInstance() {
        
    }
    
    // ########### getter/setter ###########
    
    /**
     * @return the number of machines that are available
     */
    public int getNumberMachines() {
        return this.numberMachines;
    }
    
    /**
     * @return the number of jobs that need to be assigned to machines
     */
    public int getNumberJobs() {
        if (this.jobs == null) {
            return 0;
        }
        return this.jobs.length;
    }
    
    /**
     * @return the array of jobs that need to be assigned to machines
     */
    public Job[] getJobs() {
		return jobs;
	}
    
    /**
     * @param index the index of a job
     * @return the job that has the given index
     */
    public Job getJob(int index) {
        Job job = this.jobs[index];
        if (job.getIndex() != index) {
        	for (Job j : this.jobs) {
        		if (j.getIndex() == index) {
        			return j;
        		}
        	}
        }
        return job;
    }
    
    // ########### IInstance implementation ###########
    
    /**
     * {@inheritDoc}
     */
    @Override
    public boolean parseInstance(String instancePath) throws FileNotFoundException {
        FileInputStream in = new FileInputStream(instancePath);
            
        try {
            //the first number is the number of machines
            this.numberMachines = InputStreamParser.readNextInt(in);
            if (this.numberMachines == -1) {
                this.numberMachines = 0;
                return false;
            }
            
            //the second number is the number of job sizes following this
            int numberJobs = InputStreamParser.readNextInt(in);
            if (numberJobs == -1) {
                this.numberMachines = 0;
                return false;
            }
            this.jobs = new Job[numberJobs];
            
            //read the job sizes
            for (int i = 0;i < numberJobs;i++) {
                int size = InputStreamParser.readNextInt(in);
                if (size == -1) {
                    this.numberMachines = 0;
                    this.jobs = null;
                    return false;
                }
                this.jobs[i] = new Job(i, size);
            }
            
            in.close();
        } catch (IOException ex) {
            System.err.println(ex.getMessage());
            this.numberMachines = 0;
            this.jobs = null;
            return false;
        }
        return true;
    }
    
}
